package com.jason.demo.servicehi;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zhouxinghuai
 * @description description
 * @date 2018-08-10
 */
public class DistinctTEst {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\Temp\\streamtest.txt"));
        findLongest(br);
        findDistinct(br);
        br.close();
    }

    // 查找最长的一行
    public static void findLongest(BufferedReader br) throws IOException {
        int longest = br.lines().mapToInt(String::length).max().getAsInt();
        System.out.println(longest);
    }

    // 找出全文的单词，转小写，并排序
    public static void findDistinct(BufferedReader br) {
        List<String> list =
                br.lines()
                        .flatMap(line -> Stream.of(line.split("\\s+")))
                        .filter(word -> word.length() > 0)
                        .map(String::toLowerCase).distinct().sorted()
                        .collect(Collectors.toList());

        System.out.println(list);
    }
}
